﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Web;

namespace Common
{
    public class Module_File
    {
        private string _allowedExt;
        private long _MaxSize;
        private string _UpDirectory;
        
        public Module_File()
        {
            _allowedExt = ".jpg,.jpge,.gif,.png";
            _MaxSize = 3145728;
            _UpDirectory = "upfile/";
        }
        public static string GetConfig()
        {
            if (File.Exists(HttpContext.Current.Server.MapPath("Config/config.txt")))
            {
                return File.ReadAllText(HttpContext.Current.Server.MapPath("Config/config.txt"));
            }
            return "";
        }

        public static void SaveConfig(string ConfigValue)
        {
            if (File.Exists(HttpContext.Current.Server.MapPath("Config/config.txt")))
            {
                File.WriteAllText(HttpContext.Current.Server.MapPath("Config/config.txt"), ConfigValue);
            }
        }


        /// <summary>
        /// 文件保存目录
        /// </summary>
        public string UpDirectory
        {
            get { return _UpDirectory; }
            set { _UpDirectory = value; }
        }

        /// <summary>
        /// 允许的扩展名格式：.jpg,.png,.gif
        /// </summary>
        public string allowedExt
        {
            get { return _allowedExt; }
            set { _allowedExt = value; }
        }

        /// <summary>
        /// 最大上传尺寸，字节数
        /// </summary>
        public long MaxSize
        {
            get { return _MaxSize; }
            set { _MaxSize = value; }
        }

        /// <summary>
        /// 判断扩展名是否合法
        /// </summary>
        /// <param name="_ext">要交验的扩展名如.jpg</param>
        /// <returns></returns>
        public bool CheckExt(string _ext)
        {
            if (("," + _allowedExt + ",").IndexOf("," + _ext.ToLower().Trim() + ",") == -1)
            {
                return false;
            }
            else
            {
                return true;
            }
        }

        /// <summary>
        /// 判断上传文件大小是否超标
        /// </summary>
        /// <param name="_FileSize">要上传的文件尺寸，字节数</param>
        /// <returns></returns>
        public bool CheckFileSize(long _FileSize)
        {
            if (_FileSize < MaxSize)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 建立上传目录
        /// </summary>
        public void CreateUpfileDirectory(string _TempString)
        {
            if (!Directory.Exists(_TempString))
            {
                Directory.CreateDirectory(_TempString);
            }
        }

        /// <summary>
        /// 返回目录名
        /// </summary>
        /// <returns></returns>
        public string GetDirName()
        {
            StringBuilder sb = new StringBuilder();
            DateTime dt1 = new DateTime();
            dt1 = DateTime.Now;
            sb.Append(_UpDirectory);
            sb.Append(dt1.Year.ToString());
            sb.Append(dt1.Month.ToString());
            sb.Append(dt1.Day.ToString());
            sb.Append("/");
            return sb.ToString();
        }

        /// <summary>
        /// 生成一个随机文件名
        /// </summary>
        /// <param name="_ext">扩展名</param>
        /// <returns></returns>
        public string GetFileName(string _ext)
        {
            StringBuilder sb = new StringBuilder();
            DateTime dt1 = new DateTime();
            dt1 = DateTime.Now;
            sb.Append(dt1.Year.ToString());
            sb.Append(dt1.Month.ToString());
            sb.Append(dt1.Day.ToString());
            sb.Append(dt1.Hour.ToString());
            sb.Append(dt1.Minute.ToString());
            sb.Append(dt1.Second.ToString());
            sb.Append(Utility.GetRandom(10, 99).ToString());
            sb.Append(_ext);
            return sb.ToString();
        }
    }
}
